//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.everbest.common.utils;

import java.net.NetworkInterface;
import java.security.SecureRandom;
import java.util.Enumeration;

public final class GUID {
    private static final char[] digits = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
        'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
        'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
        'v', 'w', 'x', 'y', 'z', '+', '_'};
    private static volatile SecureRandom numberGenerator = new SecureRandom();
    private static final long netHash = netHash();
    private static final int TIME_LENGTH = 7;
    private static final int NETHASH_LENGTH = 3;
    private static final int RANDOM_LENGTH = 6;
    private static final int DEFAULT_RADIO = 62;
    private static final int MIN_LENGTH = 16;
    private static long lastTime = System.currentTimeMillis();

    public GUID() {}

    public static String next(int timeLength, int netHashLength, int randomLength, int radio) {
        if (radio > digits.length) {
            radio = digits.length;
        }

        StringBuilder buffer = new StringBuilder();
        int i;
        if (randomLength > 0) {
            byte[] bytes = new byte[randomLength];
            numberGenerator.nextBytes(bytes);
            int balance = 0;

            for (i = 0; i < randomLength; ++i) {
                int v = bytes[i] & 255;
                int index = v + balance;
                index %= radio;
                balance = v / radio;
                buffer.append(digits[index]);
            }
        }

        long t;
        if (netHashLength > 0) {
            t = netHash;

            for (i = 0; i < netHashLength; ++i) {
                buffer.append(digits[(int)(t % (long)radio)]);
                t /= (long)radio;
            }
        }

        if (timeLength > 0) {
            t = System.currentTimeMillis();
            Class var13 = GUID.class;
            synchronized (GUID.class) {
                if (t > lastTime) {
                    lastTime = t;
                } else {
                    t = ++lastTime;
                }
            }

            for (i = 0; i < timeLength; ++i) {
                buffer.append(digits[(int)(t % (long)radio)]);
                t /= (long)radio;
            }
        }

        return buffer.reverse().toString();
    }

    public static String next(int length) {
        if (length < 16) {
            length = 16;
        }

        int randomLength = length - 7 - 3;
        return next(7, 3, randomLength, 62);
    }

    private static long netHash() {
        long hash = 0L;

        try {
            Enumeration nis = NetworkInterface.getNetworkInterfaces();

            while (true) {
                byte[] bytes;
                do {
                    if (!nis.hasMoreElements()) {
                        hash = Math.abs(hash);
                        return hash;
                    }

                    NetworkInterface ni = (NetworkInterface)nis.nextElement();
                    bytes = ni.getHardwareAddress();
                } while (bytes == null);

                byte[] var5 = bytes;
                int var6 = bytes.length;

                for (int var7 = 0; var7 < var6; ++var7) {
                    byte b = var5[var7];
                    hash = (hash << 5) + hash + (long)(b & 255);
                }
            }
        } catch (Exception var9) {
            hash = Math.abs(numberGenerator.nextLong());
            return hash;
        }
    }
}
